package admin.server.exception;

import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import admin.common.vo.ResponseVO;

/**
 * 全局异常处理
 * Created by zgb on 2020/2/27.
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {


    @ResponseBody
    @ExceptionHandler(value = FeignException.class)
    public ResponseVO handle(FeignException e) {
        if (e.getMessage().contains("\"status\":403")) {
            return ResponseVO.forbidden(e.getMessage());
        }
        return ResponseVO.failed(e.getMessage());
    }


    @ResponseBody
    @ExceptionHandler(value = DuplicateKeyException.class)
    public ResponseVO handle(DuplicateKeyException e) {
        if (e.getMessage().contains("Duplicate entry")) {
            return ResponseVO.failed("唯一主键冲突");
        }
        return ResponseVO.failed(e.getMessage());
    }


    @ResponseBody
    @ExceptionHandler(value = ApiException.class)
    public ResponseVO handle(ApiException e) {
        e.printStackTrace();
        log.error(e.getMessage());
        if (e.getErrorCode() != null) {
            return ResponseVO.failed(e.getErrorCode());
        }
        return ResponseVO.failed(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResponseVO handleValidException(MethodArgumentNotValidException e) {
        e.printStackTrace();
        log.error(e.getMessage());
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getField() + fieldError.getDefaultMessage();
            }
        }
        return ResponseVO.validateFailed(message);
    }

    @ResponseBody
    @ExceptionHandler(value = BindException.class)
    public ResponseVO handleValidException(BindException e) {
        e.printStackTrace();
        log.error(e.getMessage());
        BindingResult bindingResult = e.getBindingResult();
        String message = null;
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            if (fieldError != null) {
                message = fieldError.getField() + fieldError.getDefaultMessage();
            }
        }
        return ResponseVO.validateFailed(message);
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResponseVO handle(Exception e) {
        e.printStackTrace();
        log.error(e.getMessage());
        return ResponseVO.failed(e.getMessage());
    }
}
